using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;
using Weapons.Components.ComponentData;
using Weapons.Components.ComponentData.AttackData;

namespace Weapons.Components
{
    public class Damage : WeaponComponent<DamageData, AttackDamage>
    {
        [SerializeField] private float percentageDamage = 1;
        private float d1 = 7f, d2 = 0.5f, strength = 0.1f;

        private float extraDamage;

        private ActionHitBox hitBox;

        protected override void Awake()
        {
            base.Awake();

            hitBox = GetComponent(typeof(ActionHitBox)) as ActionHitBox;

            EventManager.Register<UnolockSkillEvent>(IncreaseDamage);
        }

        protected override void Start()
        {
            base.Start();
            if (hitBox != null) hitBox.OnDetectedCollider2D += HandleDetctCollider2D;
        }


        protected override void OnDestroy()
        {
            base.OnDestroy();

            hitBox.OnDetectedCollider2D -= HandleDetctCollider2D;
        }

        private void IncreaseDamage(UnolockSkillEvent evt)
        {
            var data = evt.skill.upgradeData;
            foreach (var item in data)
                if (item.statTypes == StatTypes.Strength)
                {
                    if (item.isPercentage)
                        percentageDamage = (float)(percentageDamage + item.skillIncreaseAmount * 0.01);
                    else
                        extraDamage += item.skillIncreaseAmount;
                }
        }

        private void HandleDetctCollider2D(Collider2D[] colliders)
        {
            foreach (var collider in colliders)
                if (collider.TryGetComponent(out IDamageable damageable))
                    damageable.Damage(currentAttackData.Amount * percentageDamage + extraDamage,
                        transform.parent.parent);
        }
    }
}